package com.zat.mes.andrmes.dofun.work.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.GetPackBeanResult
import com.zat.mes.andrmes.data.PackBean
import com.zat.mes.andrmes.data.PaiZaiBean
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Response

/**
 * 获取裁片包model
 */
class GetPackModel(application: Application) : AndroidViewModel(application) {

    private val packData: MutableLiveData<BaseResponse<GetPackBeanResult>> = MutableLiveData()
    private val pack: MutableLiveData<PackBean> = MutableLiveData()
    private val paiZaiData: MutableLiveData<PaiZaiBean> = MutableLiveData()
    private val packS: MutableLiveData<List<PackBean>> = MutableLiveData()
    private val packDataFail: MutableLiveData<String> = MutableLiveData()
    private val getPack: MutableLiveData<String> = MutableLiveData()
    private val postPackQty: MutableLiveData<String> = MutableLiveData()
    private val paiZaiProducedQty: MutableLiveData<String> = MutableLiveData()
    private val tranPaiZaiProducedQtyData: MutableLiveData<String> = MutableLiveData()


    fun getTranPaiZaiProducedQtyData(): LiveData<String> {
        return tranPaiZaiProducedQtyData
    }

    fun getPaiZaiProducedQty(): LiveData<String> {
        return paiZaiProducedQty
    }

    fun  getPaiZaiData(): MutableLiveData<PaiZaiBean> {
        return paiZaiData
    }

    fun getPackList(): LiveData<BaseResponse<GetPackBeanResult>> {
        return packData
    }

    fun getPackListFail(): LiveData<String> {
        return packDataFail
    }

    fun getPackGet(): LiveData<String> {
        return getPack
    }

    fun getPackUpdate(): LiveData<String> {
        return postPackQty
    }

    /**
     * 扫码获取
     */
    fun getPack(): LiveData<PackBean> {
        return pack
    }

    fun getPackS(): LiveData<List<PackBean>> {
        return packS
    }

    /**
     * 获取待领取包
     */
    fun loadGetPack(curPage: Int, pageSize: Int, factoryId: Long) {

        RetrofitManager.apiService(ApiService::class.java)
            .getPackGetList(curPage, pageSize, factoryId)
            .enqueue(object : MesCallBack<BaseResponse<GetPackBeanResult>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<GetPackBeanResult>>?,
                    response: Response<BaseResponse<GetPackBeanResult>>?
                ) {
                    packData.postValue(response?.body())
                }

                override fun onFailure(call: Call<BaseResponse<GetPackBeanResult>>, t: Throwable) {
                    super.onFailure(call, t)
                    packDataFail.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                }

            })
    }

    /**
     * 获取已领取包
     */
    fun loadGetPackHas(curPage: Int, pageSize: Int, factoryId: Long) {

        RetrofitManager.apiService(ApiService::class.java)
            .getPackGetListHas(curPage, pageSize, factoryId)
            .enqueue(object : MesCallBack<BaseResponse<GetPackBeanResult>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<GetPackBeanResult>>?,
                    response: Response<BaseResponse<GetPackBeanResult>>?
                ) {
                    packData.postValue(response?.body())
                }

                override fun onFailure(call: Call<BaseResponse<GetPackBeanResult>>, t: Throwable) {
                    super.onFailure(call, t)
                    packDataFail.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                }

            })
    }

    /**
     * 领取
     */
    fun loadGetPackGet(
        packId: Long?,
        empName: String?,
        empId: Long?,
        classId: Long?,
        className: String?
    ) {
        val jsonObject = JSONObject()
        jsonObject.put("empName", empName)
        jsonObject.put("packId", packId)
        jsonObject.put("empId", empId);
        jsonObject.put("classId", classId);
        jsonObject.put("className", className);
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.apiService(ApiService::class.java)
            .getPackGet(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    getPack.postValue(response?.body()?.message)
                }

            })

    }

    /**
     * 根据二维码查询包
     */
    fun loadPackData(qrCode: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByQrcodeGo(qrCode)
            .enqueue(object : MesCallBack<BaseResponse<List<PackBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<PackBean>>>?,
                    response: Response<BaseResponse<List<PackBean>>>?
                ) {
                    val result = response?.body()?.result
                    packS.postValue(result)
                }

            })
    }

    /**
     * 根据RFID查询包
     */
    fun loadPackRfidData(rfidCode: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByRfidGo(rfidCode)
            .enqueue(object : MesCallBack<BaseResponse<List<PackBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<PackBean>>>?,
                    response: Response<BaseResponse<List<PackBean>>>?
                ) {
                    val result = response?.body()?.result
                    packS.postValue(result)
                }

            })
    }

    /**
     *修改裁片包
     */
    fun postPackQty(packId: Long, quantity: String,weight:String,ganghao:String) {
        val jsonObject = JSONObject()
        jsonObject.put("packId", packId)
        jsonObject.put("quantity", quantity)
        jsonObject.put("remark1",ganghao)
        jsonObject.put("remark2",weight)
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.apiService(ApiService::class.java)
            .getPackUpdateQty(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    postPackQty.postValue(response?.body()?.message)
                }

            })

    }

    /**
     * 获取待办事项
     */
    fun loadGetPackById(packId: Long) {

        RetrofitManager.apiService(ApiService::class.java)
            .getPackById(packId)
            .enqueue(object : MesCallBack<BaseResponse<PackBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PackBean>>?,
                    response: Response<BaseResponse<PackBean>>?
                ) {
                    pack.postValue(response?.body()?.result)
                }


            })
    }

    /**
     *设置倒毛数量
     */
    fun postDmQty(orderNumber: String, panNo: String,quantity:String) {
        val jsonObject = JSONObject()
        jsonObject.put("orderNumber", orderNumber)
        jsonObject.put("panNo", panNo)
        jsonObject.put("quantity",quantity)
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.apiService(ApiService::class.java)
            .postDmQty(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    postPackQty.postValue(response?.body()?.message)
                }

            })
    }


    /**
     *
     */
    fun paiZaiInfo(code: String) {
       var ids= code.split(":")
        RetrofitManager.apiService(ApiService::class.java)
            .paiZaiInfo(ids.last())
            .enqueue(object : MesCallBack<BaseResponse<PaiZaiBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PaiZaiBean>>?,
                    response: Response<BaseResponse<PaiZaiBean>>?
                ) {
                    paiZaiData.postValue(response?.body()?.result)
                }
            })
    }



    /**
     *  app-设置织片包数量
     */
    fun setPaiZaiProducedQty(paiZaiId: String, produceQty: String,scrapsFlag:String) {
        val jsonObject = JSONObject()
        jsonObject.put("paiZaiId", paiZaiId)
        jsonObject.put("produceQty", produceQty)
        jsonObject.put("scrapsFlag",scrapsFlag)
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.apiService(ApiService::class.java)
            .setPaiZaiProducedQty(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    paiZaiProducedQty.postValue(response?.body()?.message)
                }

            })
    }

    /**
     * app-转移织片包数量
     */
    fun tranPaiZaiProducedQty(mainPaiZaiId: String, tranProduceQty: String,vicePaiZaiId:String) {
        val jsonObject = JSONObject()
        jsonObject.put("mainPaiZaiId", mainPaiZaiId)
        jsonObject.put("tranProduceQty", tranProduceQty)
        jsonObject.put("vicePaiZaiId",vicePaiZaiId)
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.apiService(ApiService::class.java)
            .tranPaiZaiProducedQty(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    tranPaiZaiProducedQtyData.postValue(response?.body()?.message)
                }

            })
    }




}